package com.sunshine.cloud.system;

import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;

import java.util.List;

/**
 * 代码控制自定义规则类
 *
 * @author WeiHong
 * @date 2018/11/14
 */
public class MyRule implements IRule {

    private ILoadBalancer lb;

    @Override
    public Server choose(Object o) {
        List<Server> allServers = lb.getAllServers();
        System.out.println("这是自定义服务器定规则类，输出服务器信息：");
        for (Server s : allServers) {
            System.out.println("s.getHostPort() = " + s.getHostPort());
        }
        return allServers.get(0);
    }

    @Override
    public void setLoadBalancer(ILoadBalancer iLoadBalancer) {
        this.lb = iLoadBalancer;
    }

    @Override
    public ILoadBalancer getLoadBalancer() {
        return this.lb;
    }
}
